package cn.com.platform.service;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.easy.exception.BusinessException;
import cn.com.easy.permission.model.UserModel;
import cn.com.easy.utils.DateUtils;
import cn.com.platform.dao.BlockItemDao;
import cn.com.platform.entity.BlockItemEntity;
import cn.com.platform.web.manage.dto.BlockItemDto;

/**
 * 推荐服务类
 */
@Service
public class BlockItemService {

	/** 推荐项dao */
	@Autowired
	private BlockItemDao blockItemDao;

	/**
	 * 提交推荐
	 * 
	 * @param blockItemDto
	 * @param admin
	 * @throws BusinessException
	 * @throws ParseException
	 * @author wxy 2016年8月31日
	 */
	public void saveBlockItem(BlockItemDto blockItemDto, UserModel admin) throws BusinessException, ParseException {
		if (blockItemDto.getBlockId() == null || blockItemDto.getBlockId() <= 0) {
			throw new BusinessException("推荐位不能为空");
		}
		if (blockItemDto.getOrderBy() < 0) {
			throw new BusinessException("排序不能小于0");
		}
		if (StringUtils.isBlank(blockItemDto.getCityIds())) {
			throw new BusinessException("推荐省份、城市不能为空");
		}
		String[] ids = blockItemDto.getCityIds().split(",");
		int addNum = 0; // 用于统计选中城市是否都添加
		for (String id : ids) {
			Long cityId = Long.parseLong(id);
			// 判断城市是否已推荐
			BlockItemEntity blockItemEntity = blockItemDao.findByBlockIdAndItemIdAndCityId(blockItemDto.getBlockId(), blockItemDto.getItemId(), cityId);
			if (blockItemEntity == null) {
				BlockItemEntity blockItemEntityTemp = new BlockItemEntity();
				blockItemEntityTemp.setCityId(cityId);
				blockItemEntityTemp.setItemId(blockItemDto.getItemId());
				blockItemEntityTemp.setBlockId(blockItemDto.getBlockId());
				blockItemEntityTemp.setTitle(blockItemDto.getTitle());
				blockItemEntityTemp.setLink(blockItemDto.getLink());
				blockItemEntityTemp.setPhoto(blockItemDto.getPhoto());
				if (blockItemDto.getExpireTimeString() != null && blockItemDto.getExpireTimeString().indexOf("-") > 0) {
					Date date = DateUtils.parseDate(blockItemDto.getExpireTimeString(), "yyyy-MM-dd");
					blockItemEntityTemp.setExpireTime(date);
				} else {
					blockItemEntityTemp.setExpireTime(null);// 0表示不过期
				}
				blockItemEntityTemp.setOrderBy(blockItemDto.getOrderBy());
				blockItemEntityTemp.setCreateBy(admin.getLoginname());
				blockItemDao.save(blockItemEntityTemp);
				addNum++;
			}

		}
		if (addNum == 0) {
			throw new BusinessException("选中城市都已推荐，不能重复推荐");
		}
	}

	/**
	 * 保存与修改
	 * 
	 * @param blockItemDto
	 * @param admin
	 * @throws BusinessException
	 * @throws ParseException
	 * @author lmh 2016年9月3日
	 */
	public void saveOrUpdate(BlockItemDto blockItemDto, UserModel user) throws BusinessException, ParseException {

		if (blockItemDto.getBlockId() == null || blockItemDto.getBlockId() <= 0) {
			throw new BusinessException("推荐位不能为空");
		}
		/*
		 * if (blockItemDto.getProvinceId() == null ||
		 * blockItemDto.getProvinceId() < -1 || blockItemDto.getCityId() == null
		 * || blockItemDto.getCityId() < -1) { throw new
		 * BusinessException("推荐省份、城市不能为空"); }
		 */
		if (blockItemDto.getOrderBy() < 0) {
			throw new BusinessException("排序不能小于0");
		}
		if (blockItemDto.getId() == null) {
			BlockItemEntity blockItemEntityTemp = new BlockItemEntity();
			blockItemEntityTemp.setCityId(blockItemDto.getCityId());
			blockItemEntityTemp.setBlockId(blockItemDto.getBlockId());
			blockItemEntityTemp.setProvinceId(blockItemDto.getProvinceId());
			blockItemEntityTemp.setTitle(blockItemDto.getTitle());
			blockItemEntityTemp.setLink(blockItemDto.getLink());
			blockItemEntityTemp.setPhoto(blockItemDto.getPhoto());
			blockItemEntityTemp.setItemId(blockItemDto.getItemId());
			if (StringUtils.isNotBlank(blockItemDto.getExpireTimeString()) == true && blockItemDto.getExpireTimeString().indexOf("-") > 0) {
				Date date = DateUtils.parseDate(blockItemDto.getExpireTimeString(), "yyyy-MM-dd");
				blockItemEntityTemp.setExpireTime(date);
			} else {
				blockItemEntityTemp.setExpireTime(null);// 0表示不过期
			}
			blockItemEntityTemp.setOrderBy(blockItemDto.getOrderBy());
			blockItemEntityTemp.setCreateBy(user.getLoginname());
			blockItemDao.save(blockItemEntityTemp);
		} else {
			BlockItemEntity blockItemEntityTemp = blockItemDao.findOne(blockItemDto.getId());
			if (blockItemEntityTemp == null) {
				throw new BusinessException("未找到改内容推荐");
			}
			blockItemEntityTemp.setBlockId(blockItemDto.getBlockId());
			blockItemEntityTemp.setProvinceId(blockItemDto.getProvinceId());
			blockItemEntityTemp.setCityId(blockItemDto.getCityId());
			blockItemEntityTemp.setTitle(blockItemDto.getTitle());
			blockItemEntityTemp.setLink(blockItemDto.getLink());
			blockItemEntityTemp.setItemId(blockItemDto.getItemId());
			if (StringUtils.isNotBlank(blockItemDto.getPhoto()) == true) {
				blockItemEntityTemp.setPhoto(blockItemDto.getPhoto());
			}
			if (StringUtils.isNotBlank(blockItemDto.getExpireTimeString()) == true && blockItemDto.getExpireTimeString().indexOf("-") > 0) {
				Date date = DateUtils.parseDate(blockItemDto.getExpireTimeString(), "yyyy-MM-dd");
				blockItemEntityTemp.setExpireTime(date);
			} else {
				blockItemEntityTemp.setExpireTime(null);// 0表示不过期
			}
			blockItemEntityTemp.setOrderBy(blockItemDto.getOrderBy());
			blockItemEntityTemp.setLastModifyBy(user.getLoginname());
			blockItemDao.save(blockItemEntityTemp);
		}
	}
}
